package com.bdth.weget.home.controller;

import com.bdth.weget.common.MessageResult;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.home.entity.User;
import com.bdth.weget.home.service.DepartService;
import com.bdth.weget.home.service.UserService;
import com.bdth.weget.util.MD5Utils;
import com.bdth.weget.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-13
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private DepartService departService;

    @RequestMapping(value = "index")
    public String index() {
        return "user/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<User> getPageList(HttpServletRequest request, Pagination<User> pagination,
                                    @RequestParam(required = false) String username,
                                    @RequestParam(required = false) String nickname,
                                    @RequestParam(required = false) String email){
        Map<String, Object> query = new HashMap<>();
        query.put("username", username);
        query.put("nickname", nickname);
        query.put("email", email);
        User session = (User)request.getSession().getAttribute("loginUser");
        if(session.getGroupid()!=0) {
            query.put("departids", session.getDepartids());
        }
        userService.pageLists(pagination,query);
        PageVo<User> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "form")
    public String form(){
        return "user/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(User param){
        String departids = departService.getDepartIds(param.getDepartid());
        param.setDepartids(departids);
        param.setPassword(MD5Utils.getMD5String((param.getPassword()==null ||param.getPassword()=="")?"123456":param.getPassword()));
        param.setCrtime(new Date());
        userService.insert(param);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(User param){
        String departids = departService.getDepartIds(param.getDepartid());
        param.setDepartids(departids);
        param.setUptime(new Date());
        userService.updateById(param);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        userService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            userService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "user/search";
    }
}

